//
//  StatusCellTopView.swift
//  glzbowei
//
//  Created by 高伦展 on 2018/1/7.
//  Copyright © 2018年 glz. All rights reserved.
//

import UIKit
import SnapKit
 
// 顶部视图
class StatusCellTopView: UIView {

    //微博视图模型
    var viewModel:StatusViewModel?{
        didSet{
            nameLabel.text = viewModel?.status.user?.screen_name
          
            iconView.sd_setImage(with: (viewModel?.userProfileUrl)!, placeholderImage: viewModel?.userDefaultIconView)
            
            memberIconView.image = viewModel?.userMemberImage
            
            vipIconView.image = viewModel?.userVipImage
            
            timeLabel.text = viewModel?.createAt
            
            sourceLabel.text = "glz" //viewModel?.status.source
        }
    }
    
    //MAKR:-构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MAKR:-懒加载控件
    //图片
    private lazy var iconView:UIImageView = UIImageView(imgName: "avatar_default_big")
    //名称
    private lazy var nameLabel:UILabel = UILabel(title: "glzlanson", fontSize: 14)
    //成员
    private lazy var memberIconView:UIImageView = UIImageView(imgName: "common_icon_membership_level1")
    //vip
    private lazy var vipIconView:UIImageView = UIImageView(imgName: "avatar_vip")
    //时间
    private lazy var timeLabel:UILabel = UILabel(title: "now", fontSize: 11, color:UIColor.orange)
    //来源
    private lazy var sourceLabel:UILabel = UILabel(title: "source", fontSize: 11)
    
    
}

extension StatusCellTopView{
    
    private func setupUI(){
        //backgroundColor = UIColor(white: 0.95, alpha: 0.5)
        
        //0.添加分隔视图
        let sepView = UIView()
        sepView.backgroundColor = UIColor.lightGray
        
        addSubview(sepView)
        
        sepView.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.top)
            make.left.equalTo(self.snp.left)
            make.right.equalTo(self.snp.right)
            make.height.equalTo(StatusCellMargin)
        }
         
        //1.添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        
        //2.自动布局
        iconView.snp.makeConstraints { (make) in
            make.top.equalTo(sepView.snp.bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp.left).offset(StatusCellMargin)
            make.width.equalTo(StatusCellIconWidth)
            make.height.equalTo(StatusCellIconWidth)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp.top)
            make.left.equalTo(iconView.snp.right).offset(StatusCellMargin)
        }
        
        memberIconView.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.top)
            make.left.equalTo(nameLabel.snp.right).offset(StatusCellMargin)
        }
        
        vipIconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp.right)
            make.centerY.equalTo(iconView.snp.bottom)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(iconView.snp.bottom)
            make.left.equalTo(iconView.snp.right).offset(StatusCellMargin)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(timeLabel.snp.bottom)
            make.left.equalTo(timeLabel.snp.right).offset(StatusCellMargin)
        }
    }
}
